package com.asteros.crypto.utils.content;

import ru.signalcom.util.encoders.Base64;

import com.asteros.common.util.http.Client;
import com.asteros.common.util.http.HttpResponse;
import com.asteros.common.util.io.IOUtil;

public final class RemoteCM_servlet implements ContentManager
{
	protected static final String CONTENT_PATH 		= "C:\\1\\1\\user\\";
	protected static final String CONTENT_FILE_NAME = CONTENT_PATH + "content.pdf";
	protected static final String SIGN_FILE_NAME 	= CONTENT_PATH + "1.sgn";
	
	public byte[] getContent(String userId, String documentId) throws Exception
	{
		System.out.println("RemoteContentManager.getContent(applet-servlet)>>");

		byte[] buf = null;
		Client httpClient = new Client(getContentUrl(userId, documentId));

		HttpResponse response = httpClient.call("", "GET");

		if (response.is200ResponseCode()) {
			buf = Base64.decode(response.getData());
//			buf = Base64Util.decode(response.getData());
			System.out.println("RemoteContentManager.getContent.size=" + buf.length);

			IOUtil.saveFile(buf, CONTENT_FILE_NAME);
		}

		System.out.println("RemoteContentManager.getContent(applet-servlet)<<");
		return buf;
	}
	
	public boolean verifyDocumentSignature(String userId, String documentId) throws Exception
	{
		System.out.println("RemoteContentManager.verifyDocumentSignature(applet-servlet)>>");

		boolean res = false;
		Client httpClient = new Client(getVerifyDocumentSignatureUrl(userId, documentId));

		HttpResponse response = httpClient.call("", "GET");

		if (response.is200ResponseCode()) {
			res = Boolean.valueOf(response.getData());
			System.out.println("RemoteContentManager.verifyDocumentSignature.size=" + response.getData());
		}

		System.out.println("RemoteContentManager.verifyDocumentSignature(applet-servlet)<<");
		return res;	
	}
	
	public byte[] getSignedData(String userId, String documentId) throws Exception
	{
		System.out.println("RemoteContentManager.getSignedData(applet-servlet)>>");

		byte[] buf = null;
		Client httpClient = new Client(getSignedDataSaveUrl(userId, documentId));

		HttpResponse response = httpClient.call("", "GET");

		if (response.is200ResponseCode()) {
			buf = Base64.decode(response.getData());
//			buf = Base64Util.decode(response.getData());
			System.out.println("RemoteContentManager.getContent.size=" + buf.length);

			IOUtil.saveFile(buf, "C:\\projects\\rkk\\user\\signedData_from_server.txt");
		}

		System.out.println("RemoteContentManager.getSignedData(applet-servlet)<<");
		return buf;
	}

	public void saveSignedData(String userId, String documentId, byte[] signedData) throws Exception {
		System.out.println("RemoteContentManager.saveSignedData>>buf.len" + signedData.length);

		Client httpClient = new Client(getSignedDataSaveUrl(userId, documentId));
//		String docEncoded = Base64Util.encode(buf);
		String docEncoded = Base64.encode(signedData).toString();
		byte[] a = Base64.decode(docEncoded);
		
		HttpResponse response = httpClient.call(docEncoded, "POST");

		if ((response.is201ResponseCode()) || (response.is200ResponseCode())) {
			String resultCall = response.getData();
			System.out.println("RemoteContentManager.saveTSPSign.getResponseContentType=" + response.getContentType());
			System.out.println("RemoteContentManager.saveTSPSign.getResponseLocation=" + response.getLocation());
		}

		System.out.println("RemoteContentManager.saveSignedData<<");
	}

/*	public byte[] getSign(String userId, String documentId)
	throws Exception
	{
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public void saveSign(String userId, String documentId, byte[] buf)
	throws Exception
	{
		System.out.println("RemoteContentManager.saveSign>>buf.len" + buf.length);

		Client httpClient = new Client(getSaveSignUrl(userId, documentId));
		String docEncoded = Base64Util.encode(buf);
		HttpResponse response = httpClient.call(docEncoded, "POST");

		if ((response.is201ResponseCode()) || (response.is200ResponseCode())) {
			String resultCall = response.getData();
			System.out.println("RemoteContentManager.savesign.getResponseContentType=" + response.getContentType());
			System.out.println("RemoteContentManager.savesign.getResponseLocation=" + response.getLocation());
		}

		System.out.println("RemoteContentManager.saveSign<<");
	}
*/
/*	public byte[] getTSPSign(String userId, String documentId) throws Exception
	{
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public void saveTSPSign(String userId, String documentId, byte[] buf) throws Exception {
		System.out.println("RemoteContentManager.saveTSPSign>>buf.len" + buf.length);

		Client httpClient = new Client(getSaveTSPSignUrl(userId, documentId));
		String docEncoded = Base64Util.encode(buf);
		HttpResponse response = httpClient.call(docEncoded, "POST");

		if ((response.is201ResponseCode()) || (response.is200ResponseCode())) {
			String resultCall = response.getData();
			System.out.println("RemoteContentManager.saveTSPSign.getResponseContentType=" + response.getContentType());
			System.out.println("RemoteContentManager.saveTSPSign.getResponseLocation=" + response.getLocation());
		}

		System.out.println("RemoteContentManager.saveTSPSign<<");
	}*/

	private String getServerUrl()
	{
		return "http://jcp-server.etops.ru:8080/websignserver/tunnel";
//		return "http://127.0.0.1:8080/websignserver";
	}

	private String getContentUrl(String userId, String documentId)
	{
		String query = getServerUrl() + "?action=getdocument&did=1";
		return query;
	}
private String getVerifyDocumentSignatureUrl(String userId, String documentId) {
		String query = getServerUrl() + "?action=verifydocumentsignature&did=1&content=signcontent";
		return query;
	}

	/*
	private String getSaveSignUrl(String userId, String documentId) {
		String query = getServerUrl() + "?action=savesign&did=1&content=signcontent";
		return query;
	}

	private String getSaveTSPSignUrl(String userId, String documentId) {
		String query = getServerUrl() + "?action=savetspsign&did=1&content=signcontent";
		return query;
	}
*/
	private String getSignedDataGetUrl(String userId, String documentId) {
		String query = getServerUrl() + "?action=getsigneddata&did=1&content=signcontent";
		return query;
	}
	private String getSignedDataSaveUrl(String userId, String documentId) {
		String query = getServerUrl() + "?action=savesigneddata&did=1&content=signcontent";
		return query;
	}

	private String getVerifyDocumentUrl(String userId, String documentId) {
		String query = getServerUrl() + "?action=verifydocumentsignature&did=1&content=signcontent";
		return query;
	}
}

/* Location:           C:\Users\Kirill\Desktop\esign\esign\esign.jar
 * Qualified Name:     com.topsbi.crypto.RemoteContentManager
 * JD-Core Version:    0.6.0
 */